<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }
        .span{
            position: absolute;
            width: 1px;
            height: 1000px;
            background-color: black;
            left: 1000px;
            top: 0px;
        }
    </style>
    <script>
        window.onload = function(){
            var btn = document.getElementById("btn");
            var box = document.getElementsByClassName("box")[0];
            var siv;
            var left = 0;
            btn.onclick = function(){
                // 开启定时器前先关闭上一个定时器
                clearInterval(siv);

                // 设置定时器
                siv = setInterval(function(){
                    left += 19;
                    if (left >= 900){
                        left = 900;
                    }
                    // 修改box的位置
                    box.style.left = left+"px";
                    // 设置box的移动边界
                    if (left == 900){
                        clearInterval(siv);
                    }
                },50);
            }
        }
    </script>
</head>
<body>
    <button id="btn">开始</button>
    <div class="box"></div>
    <div class="span"></div>
</body>
</html>